package turtle.daily;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import arbor.mining.util.date.DateUtil;
import arbor.mining.util.net.*;
import arbor.mining.util.stock.*;
public class DBMakerCrawler{			
    public static final String ELE1_FN = "data/ELE1.csv";
    public static final String FIN1_FN = "data/FIN1.csv";
    public static final String STX1_FN = "data/STX1.csv";
    public static final String WTX0_FN = "data/WTX0.csv";
    public static final String WTX1_FN = "data/WTX1.csv";
    
    public DBMakerCrawler(boolean retrieveHistory) {
    	if (retrieveHistory) {
    	  DBMakerGather gather = new DBMakerGather();
    	  ArrayList<StockEntity> ele1 = gather.getIndexHistory(DBMakerGather.ELE1_ID);
    	  initialSave2File(ele1, ELE1_FN);
    	  ArrayList<StockEntity> fin1 = gather.getIndexHistory(DBMakerGather.FIN1_ID);
    	  initialSave2File(fin1, FIN1_FN);
    	  ArrayList<StockEntity> stx1 = gather.getIndexHistory(DBMakerGather.STX1_ID);
    	  initialSave2File(stx1, STX1_FN);
    	  ArrayList<StockEntity> wtx0 = gather.getIndexHistory(DBMakerGather.WTX0_ID);
    	  initialSave2File(wtx0, WTX0_FN);
    	  ArrayList<StockEntity> wtx1 = gather.getIndexHistory(DBMakerGather.WTX1_ID);
    	  initialSave2File(wtx1, WTX1_FN);
    	  
    	}
    	else {
      	  DBMakerGather gather = new DBMakerGather();
      	  StockEntity ele1 = gather.getTodayIndexPrice(DBMakerGather.ELE1_ID);
    	  dailySave2File(ele1, ELE1_FN);
    	  StockEntity fin1 = gather.getTodayIndexPrice(DBMakerGather.FIN1_ID);
    	  dailySave2File(fin1, FIN1_FN);
    	  StockEntity stx1 = gather.getTodayIndexPrice(DBMakerGather.STX1_ID);
    	  dailySave2File(stx1, STX1_FN);
    	  StockEntity wtx0 = gather.getTodayIndexPrice(DBMakerGather.WTX0_ID);
    	  dailySave2File(wtx0, WTX0_FN);
    	  StockEntity wtx1 = gather.getTodayIndexPrice(DBMakerGather.WTX1_ID);
    	  dailySave2File(wtx1, WTX1_FN);
    	}
    }
   
    private void initialSave2File(ArrayList<StockEntity> entities, String fileName) {
    	DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    	try {
    	  BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    	  for (int i=0;i<entities.size();i++) {
    		  StockEntity entity = entities.get(i);
    		  writer.write(df.format(entity.startTime.getTime())+",");
    		  writer.write(entity.ohlc.high+",");
    		  writer.write(entity.ohlc.low+",");
    		  writer.write(entity.ohlc.open+",");
    		  writer.write(entity.ohlc.close+",");
    		  writer.write(entity.volume+"\n");
    	  }
    	  writer.flush();
    	  writer.close();
    	} catch (IOException e) {
    		System.out.println(e);
    	}
    }
    private void dailySave2File(StockEntity entity, String fileName) {
    	DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    	try {
      	  BufferedWriter writer = new BufferedWriter(new FileWriter(fileName,true));
		  writer.write(df.format(entity.startTime.getTime())+",");
		  writer.write(entity.ohlc.high+",");
		  writer.write(entity.ohlc.low+",");
		  writer.write(entity.ohlc.open+",");
		  writer.write(entity.ohlc.close+",");
		  writer.write(entity.volume+"\n");      	  
      	  writer.flush();
      	  writer.close();
      	} catch (IOException e) {
      		System.out.println(e);
      	}
    }
    public static void main(String[] args) {
    	new DBMakerCrawler(true);
    }
    
}